package label

import (
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/dao"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/entity"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/global"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/model"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"strconv"
	"strings"
)

//********************************************
//Func  ：查询所有标签列表
//Method：GET
//Usage ：
//********************************************
func QueryAllLabels(c *gin.Context) {
	pageSize, err1 := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	if err1 != nil {
		global.Log.Error("QueryAllLabels input param pagesize error:", zap.Any("error", err1))
		c.JSON(http.StatusOK, model.Fail("请求参数错误"))
		return
	}

	page, err2 := strconv.Atoi(c.DefaultQuery("page", "1"))
	if err2 != nil {
		global.Log.Error("QueryAllLabels input param page error:", zap.Any("error", err2))
		c.JSON(http.StatusOK, model.Fail("请求参数错误"))
		return
	}

	total, labels, err := dao.QueryLabelList(page, pageSize)
	if err != nil {
		global.Log.Error("QueryAllLabels failed.", zap.Any("error", err))
		c.JSON(http.StatusOK, model.Fail("查询标签失败"))
	} else {
		type LabelList struct { //列表查询输出
			Total int64
			List  []*entity.Label
		}

		output := &LabelList{
			Total: total,
			List:  labels,
		}

		c.JSON(http.StatusOK, model.SuccessObject("操作成功", output))
		global.Log.Info("QueryAllLabels success.", zap.Any("label list", output))
	}
}

//********************************************
//Func  ：查询指定标签详情
//Method：GET
//Usage ：
//********************************************
func QueryLabelDetail(c *gin.Context) {
	deptId, err := strconv.ParseInt(c.DefaultQuery("id", ""), 10, 64)
	if err != nil {
		global.Log.Error("QueryLabelDetail id is lost.")
		c.JSON(http.StatusOK, model.Fail("未指明查询标签"))
		return
	}
	if deptId <= 0 {
		global.Log.Error("QueryLabelDetail deptid is lost.")
		c.JSON(http.StatusOK, model.Fail("未指明查询标签"))
		return
	}

	label, err := dao.QueryLabelDetail(deptId)
	if err != nil {
		global.Log.Error("QueryLabelDetail failed.", zap.Any("error", err))
		c.JSON(http.StatusOK, model.Fail("查询标签详情失败"))
	} else {
		c.JSON(http.StatusOK, model.SuccessObject("操作成功", label))
		global.Log.Info("QueryLabelDetail success.", zap.Any("label detail", label))
	}
}

//********************************************
//Func  ：添加新标签
//Method：POST
//Usage ：
//********************************************
func AddNewLabel(c *gin.Context) {
	var label entity.Label
	var err error
	label.Label = strings.TrimSpace(c.DefaultPostForm("label", "")) //标题
	if len(label.Label) <= 0 {
		global.Log.Error("AddNewLabel name is none.")
		c.JSON(http.StatusOK, model.Fail("标签名称不能为空"))
		return
	}

	label.Author, err = strconv.ParseInt(c.DefaultPostForm("author", ""), 10, 64)
	label.Remark = c.DefaultPostForm("remark", "")

	newlabel, err := dao.CreateLabel(label)
	if err != nil {
		if strings.Contains(err.Error(), "Duplicate") {
			global.Log.Error("AddNewLabel failed.", zap.Any("error", err))
			c.JSON(http.StatusOK, model.Fail("该标签已存在"))
		} else {
			global.Log.Error("AddNewLabel failed.", zap.Any("error", err))
			c.JSON(http.StatusOK, model.Fail("添加标签失败"))
		}
	} else {
		c.JSON(http.StatusOK, model.SuccessObject("添加标签成功", newlabel))
		global.Log.Info("AddNewLabel success.", zap.Any("new label", newlabel))
	}
}

//********************************************
//Func  ：更新标签信息
//Method：POST
//Usage ：
//********************************************
func UpdateLabel(c *gin.Context) {
	var label entity.Label
	var err error
	label.Label = strings.TrimSpace(c.DefaultPostForm("label", "")) //标题
	if len(label.Label) <= 0 {
		global.Log.Error("UpdateLabel label is none.")
		c.JSON(http.StatusOK, model.Fail("标签名称不能为空"))
		return
	}

	label.Id, err = strconv.ParseInt(c.DefaultPostForm("id", ""), 10, 64)
	if err != nil {
		global.Log.Error("label id is lost.")
		c.JSON(http.StatusOK, model.Fail("更新标签信息失败"))
	}
	if label.Id <= 0 {
		global.Log.Error("UpdateLabel id is none.")
		c.JSON(http.StatusOK, model.Fail("更新标签信息失败"))
		return
	}

	label.Remark = c.DefaultPostForm("remark", "")

	err = dao.UpdateLabel(label)
	if err != nil {
		global.Log.Error("UpdateLabel failed.", zap.Any("error", err))
		c.JSON(http.StatusOK, model.Fail("更新标签失败"))
	} else {
		c.JSON(http.StatusOK, model.SuccessObject("操作成功", label))
		global.Log.Info("UpdateLabel success.", zap.Any("new label", label))
	}
}

//********************************************
//Func  ：用户删除
//Method：DELETE
//Usage ：留言板页面-个人设置-账号注销
//********************************************
func DeleteLabel(c *gin.Context) {
	id, err := strconv.ParseInt(c.DefaultQuery("id", ""), 10, 64)
	if err != nil {
		global.Log.Error("DeleteLabel label id is lost.")
		c.JSON(http.StatusOK, model.Fail("删除标签失败"))
		return
	}

	err = dao.DeleteLabel(id)
	if err != nil {
		global.Log.Error("DeleteLabel failed.", zap.Any("error", err))
		c.JSON(http.StatusOK, model.Fail("标签删除失败"))
	} else {
		c.JSON(http.StatusOK, model.Success("操作成功"))
		global.Log.Info("DeleteLabel success.", zap.Any("delete label id", id))
	}
}

//********************************************
//Func  ：添加新标签到工单
//Method：POST
//Usage ：
//********************************************
func MarkLabelToWorkOrder(c *gin.Context) {
	var err error
	labelId, err := strconv.ParseInt(c.DefaultPostForm("labelId", ""), 10, 64)
	if err != nil || labelId <= 0 {
		global.Log.Error("MarkLabelToWorkOrder input params failed.",
			zap.Any("error", err), zap.Any("labelId", labelId))
		c.JSON(http.StatusOK, model.Fail("未指明标签"))
		return
	}
	workerOrderId, err := strconv.ParseInt(c.DefaultPostForm("workerOrderId", ""), 10, 64)
	if err != nil || workerOrderId <= 0 {
		global.Log.Error("MarkLabelToWorkOrder input params failed.",
			zap.Any("error", err), zap.Any("workerOrderId", workerOrderId))
		c.JSON(http.StatusOK, model.Fail("未指明工单"))
		return
	}

	err = dao.CreateLabelOrderObj(labelId, workerOrderId)
	if err != nil {
		global.Log.Error("CreateLabelOrderObj failed.", zap.Any("error", err))
		c.JSON(http.StatusOK, model.Fail("添加标签失败"))
	} else {
		c.JSON(http.StatusOK, model.Success("打标签成功"))
		global.Log.Info("AddNewLabel success.")
	}
}

//********************************************
//Func  ：工单移除标签
//Method：POST
//Usage ：
//********************************************
func ClearLabelFromWorkOrder(c *gin.Context) {
	var err error
	labelId, err := strconv.ParseInt(c.DefaultPostForm("labelId", ""), 10, 64)
	if err != nil || labelId <= 0 {
		global.Log.Error("ClearLabelFromWorkOrder input params failed.",
			zap.Any("error", err), zap.Any("labelId", labelId))
		c.JSON(http.StatusOK, model.Fail("未指明标签"))
		return
	}
	workerOrderId, err := strconv.ParseInt(c.DefaultPostForm("workerOrderId", ""), 10, 64)
	if err != nil || workerOrderId <= 0 {
		global.Log.Error("ClearLabelFromWorkOrder input params failed.",
			zap.Any("error", err), zap.Any("workerOrderId", workerOrderId))
		c.JSON(http.StatusOK, model.Fail("未指明工单"))
		return
	}

	err = dao.DeleteLabelOrderObj(labelId, workerOrderId)
	if err != nil {
		global.Log.Error("DeleteLabelOrderObj failed.", zap.Any("error", err))
		c.JSON(http.StatusOK, model.Fail("移除标签失败"))
	} else {
		c.JSON(http.StatusOK, model.Success("移除标签成功"))
		global.Log.Info("ClearLabelFromWorkOrder success.")
	}
}
